﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NetCounter_CountShow : System.Web.UI.Page
{
    public string title = "";
    public string strval = "";
    private inc ii = new inc();
    private string url = "CountShow.aspx";
    public string action = "admin";
    protected void Page_Load(object sender, System.EventArgs e)
    {
        this.btn_submit.Attributes.Add("onclick", "return ckform1();");
        if (!this.IsPostBack)
        {
            try
            {
                url = Request.Url.ToString();
                if (url.IndexOf("?") > 0)
                    url = url.Substring(0, url.IndexOf("?"));

                if (Request["act"] == null)
                    action = "main";
                else
                    action = Request["act"].ToString();

                switch (action.ToLower())
                {
                    case "admin":
                        Sb_Admin();
                        break;
                    case "main":
                        Sb_Main();
                        break;
                    case "hour":
                        Sb_Hour();
                        break;
                    case "day":
                        Sb_Day();
                        break;
                    case "month":
                        Sb_Month();
                        break;
                    case "osexp":
                        Sb_OSExp();
                        break;
                    case "where":
                        Sb_Where();
                        break;
                    case "referer":
                        Sb_Referer();
                        break;
                    case "last20":
                        Sb_Last20();
                        break;
                    default:
                        Sb_Main();
                        action = "main";
                        break;
                }

            }
            catch (Exception ex)
            {
                Response.Write("ERR:" + ex.Message);
            }
        }
    }

    /// <summary>
    /// 管理网站计数
    /// </summary>
    private void Sb_Admin()
    {
        try
        {
            if (ii == null)
                return;

            DataTable dt = new DataTable();
            dt = ii.Find("select * from WebInfo");
            if (dt.Rows.Count > 0)
            {
                this.WebName.Value = dt.Rows[0]["WebName"].ToString();
                this.WebUrl.Value = dt.Rows[0]["WebUrl"].ToString();
                this.WebMaster.Value = dt.Rows[0]["WebMaster"].ToString();
                this.WebEmail.Value = dt.Rows[0]["WebEmail"].ToString();
                this.WebExp.Value = dt.Rows[0]["WebExp"].ToString();
            }
            dt = ii.Find("select count(*) as counts from Last20Vister");
            string ct = dt.Rows[0][0].ToString().Trim();
            if (ct == "")
                ct = "20";
            this.lastnum.Value = ct;
        }
        catch (Exception ex)
        {
            Response.Write("ERR:" + ex.Message);
            return;
        }
    }

    /// <summary>
    /// 网站统计简要信息
    /// </summary>
    private void Sb_Main()
    {
        //取得基本资料
        string vSql = "select WebName,WebUrl,WebExp,WebEmail,WebMaster,WebCountTime from webinfo";
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(vSql);
        string WebName = "";
        string WebUrl = "";
        string WebExp = "";
        string WebEmail = "";
        string WebMaster = "";
        string WebCountTime = "";
        if (dt1.Rows.Count > 0)
        {
            WebName = dt1.Rows[0]["WebName"].ToString();
            WebUrl = dt1.Rows[0]["WebUrl"].ToString();
            WebExp = dt1.Rows[0]["WebExp"].ToString();
            WebEmail = dt1.Rows[0]["WebEmail"].ToString();
            WebMaster = dt1.Rows[0]["WebMaster"].ToString();
            WebCountTime = dt1.Rows[0]["WebCountTime"].ToString();
        }
        else
            Response.Redirect(url + "?act=admin");
        dt1.Dispose();

        //统计总人数
        vSql = "select Sum(cCMC) as vCount from cM";
        dt1 = ii.Find(vSql);
        int vCount = 0;
        if (dt1.Rows[0]["vCount"].ToString() != "")
            vCount = Int32.Parse(dt1.Rows[0]["vCount"].ToString());
        dt1.Dispose();

        //统计今日访问量和计算今日访问量预测
        string mt = DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
        double vCha = double.Parse(mt) / 24;

        vSql = "select cTDC from cD where cDay =" + DateTime.Now.Day;
        dt1 = ii.Find(vSql);
        int vToday = 0;
        if (dt1.Rows.Count > 0)
            vToday = Int32.Parse(dt1.Rows[0]["cTDC"].ToString());
        dt1.Dispose();
        double vYToday = Math.Round(vToday / vCha * (1 - vCha) + vToday);

        //统计天数和平均日访问量
        string vCDate = (DateTime.Now - DateTime.Parse(WebCountTime)).ToString();
        if (vCDate.IndexOf(".") > 0)
            vCDate = vCDate.Substring(0, vCDate.IndexOf("."));
        else
            vCDate = "0";
        double vAveCount = 0;
        if (vCDate != "0")
        {
            vAveCount = (vCount - vToday) / Int32.Parse(vCDate);
            vAveCount = Math.Round(vAveCount);
        }

        //年访问量和月访问量
        vSql = "select cTMC from cM where cMonth=" + DateTime.Now.Month;
        dt1 = ii.Find(vSql);
        string vMCount = "0";
        if (dt1.Rows.Count > 0)
            vMCount = dt1.Rows[0]["cTMC"].ToString();

        vSql = "select refurbish from flux";
        dt1 = ii.Find(vSql);
        string vref = "0";
        if (dt1.Rows.Count > 0)
            vref = dt1.Rows[0]["refurbish"].ToString();

        dt1.Dispose();

        string str = "<table align='center' width=98% border=0 cellpadding=2><tr><td>总访问人数：</td><td width=41% >" + vCount + "</td><td width=14% >开始统计日期：</td><td width=31% >" + WebCountTime + "</td></tr><tr><td>今日访问量：</td><td>" + vToday + "</td><td>本月访问量：</td><td>" + vMCount + "</td></tr><tr><td>统计天数：</td><td>" + vCDate + "</td><td>平均日访问量：</td><td>" + vAveCount + "</td></tr><td>整站页面刷新：</td><td>" + vref + "</td><td>预计本日访问量：</td><td>" + vYToday + "</td></tr></table><br>";
        title = "网站统计简要信息";
        strval = str;
    }

    /// <summary>
    /// 24小时统计
    /// </summary>
    public void Sb_Hour()
    {
        //取的当前小时数
        int vHour = DateTime.Now.Hour;
        //循环取最近24小时的访问人数
        ii.ExecBool("update cH set cTHC=0,HMD=now() where DATEDIFF('h',HMD,Now()) > 23 ");

        int MaxHour = 0;
        string vSql = "select cH.cHour,cH.cTHC,MaxNum.MaxCount from cH,(select Max(cTHC) as MaxCount from cH) as MaxNum order by cH.cHour asc";
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(vSql);
        ArrayList aHour = new ArrayList(23);
        for (int i = 0; i <= 23 && i < dt1.Rows.Count; i++)
        {
            aHour.Add(dt1.Rows[i]["cTHC"].ToString());
            MaxHour = Int32.Parse(dt1.Rows[i]["MaxCount"].ToString());
        }

        //算出图例最大值的比例
        if (MaxHour == 0)
            MaxHour = 1;
        double vPoint = 100.00 / MaxHour;
        int k = 0;
        int avgcount = 0;
        string sImage = "<table border=0 cellpadding=2><tr><td align=center>最近24小时统计图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxHour + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.25 + "</td></tr></table></td>";
        for (int vPH = vHour + 1; vPH <= 23; vPH++)
        {
            k = k + 1;
            double sHeight = Int32.Parse(aHour[vPH].ToString()) * vPoint;
            avgcount = avgcount + Int32.Parse(aHour[vPH].ToString());
            double avgheight = (avgcount / k) * vPoint;
            sImage = sImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + " id=htav avgheight=" + avgheight + " title=访问量：" + aHour[vPH].ToString() + "><br>" + vPH + "</td>";
        }
        for (int vBH = 0; vBH <= vHour; vBH++)
        {
            k = k + 1;
            double sHeight = Int32.Parse(aHour[vBH].ToString()) * vPoint;
            avgcount = avgcount + Int32.Parse(aHour[vBH].ToString());
            double avgheight = (avgcount / k) * vPoint;
            sImage = sImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + " id=htav  avgheight=" + avgheight + " title=访问量：" + aHour[vBH].ToString() + "><br>" + vBH + "</td>";
        }
        sImage = sImage + "<td>单位（点）</td></tr></table></td></tr></table>";

        //取出所有24小四的统计图表
        vSql = "select cH.cHour,cH.cCHC,MaxNum.MaxCount from cH,(select Max(cCHC) as MaxCount from cH) as MaxNum order by cH.cHour asc";
        dt1 = ii.Find(vSql);
        MaxHour = 0;
        for (int i = 0; i <= 23; i++)
        {
            aHour[i] = dt1.Rows[i]["cCHC"].ToString();
            MaxHour = Int32.Parse(dt1.Rows[i]["MaxCount"].ToString());
        }

        //算出图例最大值的比例
        if (MaxHour == 0)
            MaxHour = 1;
        vPoint = 100.00 / MaxHour;
        sImage = sImage + "<table border=0 cellpadding=2><tr><td align=center>访问量24小时分配图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxHour + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxHour * 0.25 + "</td></tr></table></td>";
        for (int vL = 0; vL <= 23; vL++)
        {
            double sHeight = Int32.Parse(aHour[vL].ToString()) * vPoint;
            sImage = sImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + "  title=访问量：" + aHour[vL].ToString() + "><br>" + vL + "</td>";
        }
        sImage = sImage + "<td>单位（点）</td></tr></table></td></tr></table>";

        aHour.Clear();  //释放掉数组
        dt1.Dispose();

        title = "24小时统计信息";
        strval = sImage;
    }

    /// <summary>
    /// 日统计
    /// </summary>
    private void Sb_Day()
    {
        int vDay = DateTime.Now.Day;
        //循环取最近24小时的访问人数

        int MaxCDay = 0;
        int MaxDay = Fn_MaxDay(DateTime.Now.AddMonths(-1).Month);
        ii.ExecBool("update cD set cTDC=0,DMD=now() where DATEDIFF('d',DMD,Now()) > " + MaxDay);
        ArrayList aDay = new ArrayList(31);
        aDay.Add("0");

        string vSql = "select cD.cDay,cD.cTDC,MaxNum.MaxCount from cD,(select Max(cTDC) as MaxCount from cD) as MaxNum order by cD.cDay asc";
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(vSql);
        for (int i = 1; i <= 31 && i <= dt1.Rows.Count; i++)
        {
            aDay.Add(dt1.Rows[i - 1]["cTDC"].ToString());
            MaxCDay = Int32.Parse(dt1.Rows[i - 1]["MaxCount"].ToString());
        }

        //算出图例最大值的比例
        if (MaxCDay == 0)
            MaxCDay = 1;
        double vPoint = 100.00 / MaxCDay;
        int k = 0;
        int avgcount = 0;
        string sDImage = "<table border=0 cellpadding=2><tr><td align=center>最近30天统计图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxCDay + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.25 + "</td></tr></table></td>";
        for (int vL = vDay + 1; vL <= MaxDay; vL++)
        {
            k = k + 1;
            double sHeight = Int32.Parse(aDay[vL].ToString()) * vPoint;
            avgcount = avgcount + Int32.Parse(aDay[vL].ToString());
            double avgheight = (avgcount / k) * vPoint;
            sDImage = sDImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + " id=htav  avgheight=" + avgheight + "  title=访问量：" + aDay[vL].ToString() + "><br>" + vL + "</td>";
        }
        for (int vL = 1; vL <= vDay; vL++)
        {
            k = k + 1;
            double sHeight = Int32.Parse(aDay[vL].ToString()) * vPoint;
            avgcount = avgcount + Int32.Parse(aDay[vL].ToString());
            double avgheight = (avgcount / k) * vPoint;
            sDImage = sDImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + " id=htav  avgheight=" + avgheight + "  title=访问量：" + aDay[vL].ToString() + "><br>" + vL + "</td>";
        }
        sDImage = sDImage + "</tr></table></td></tr></table>";

        //取出所有24小四的统计图表
        MaxDay = Fn_MaxDay(DateTime.Now.Month) - 1;

        MaxCDay = 0;
        vSql = "select cD.cDay,cD.cCDC,MaxNum.MaxCount from cD,(select Max(cCDC) as MaxCount from cD) as MaxNum order by cD.cDay asc";
        dt1 = ii.Find(vSql);
        aDay.Clear();
        aDay.Add("0");
        for (int i = 1; i <= 31 && i <= dt1.Rows.Count; i++)
        {
            aDay.Add(dt1.Rows[i - 1]["cCDC"].ToString());
            MaxCDay = Int32.Parse(dt1.Rows[i - 1]["MaxCount"].ToString());
        }

        //算出图例最大值的比例
        if (MaxCDay == 0)
            MaxCDay = 1;
        vPoint = 100.00 / MaxCDay;
        sDImage = sDImage + "<table border=0 cellpadding=2><tr><td align=center>访问量各天分配图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxCDay + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxCDay * 0.25 + "</td></tr></table></td>";
        for (int vL = 1; vL <= 31; vL++)
        {
            double sHeight = Int32.Parse(aDay[vL].ToString()) * vPoint;
            sDImage = sDImage + "<td align=center width=15 background=images/tu_back.gif height=120 nowrap><img src=images/bar.gif width=15 height=" + sHeight + " title=访问量：" + aDay[vL].ToString() + "><br>" + vL + "</td>";
        }
        sDImage = sDImage + "</tr></table></td></tr></table>";

        aDay.Clear();
        dt1.Dispose();

        title = "按日统计信息";
        strval = sDImage;

    }

    /// <summary>
    /// 月统计
    /// </summary>
    private void Sb_Month()
    {
        //取的当前月份数
        int vMonth = DateTime.Now.Month;

        ii.ExecBool("update cM set cTMC=0,MMD=now() where DATEDIFF('m',MMD,Now()) > 11");
        ArrayList aMonth = new ArrayList(12);
        int MaxMonth = 0;

        //循环取最近24小时的访问人数
        string vSql = "select cM.cMonth,cM.cTMC,MaxNum.MaxCount from cM,(select Max(cTMC) as MaxCount from cM) as MaxNum order by cM.cMonth asc";
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(vSql);
        aMonth.Add("0");
        for (int vS = 1; vS <= 12; vS++)
        {
            aMonth.Add(dt1.Rows[vS - 1]["cTMC"].ToString());
            MaxMonth = Int32.Parse(dt1.Rows[vS - 1]["MaxCount"].ToString());
        }
        //算出图例最大值的比例
        if (MaxMonth == 0)
            MaxMonth = 1;
        double vPoint = 100.00 / MaxMonth;
        string sMImage = "<table border=0 cellpadding=2><tr><td align=center>最近12个月统计图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxMonth + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.25 + "</td></tr></table></td>";
        for (int vL = vMonth + 1; vL <= 12; vL++)
        {
            double sHeight = Int32.Parse(aMonth[vL].ToString()) * vPoint;
            sMImage = sMImage + "<td align=center width=15 background=images/tu_back.gif height=120><img src=images/bar.gif width=15 height=" + sHeight + "  title=访问量：" + aMonth[vL].ToString() + "><br>" + vL + "</td>";

        }
        for (int vL = 1; vL <= vMonth; vL++)
        {
            double sHeight = Int32.Parse(aMonth[vL].ToString()) * vPoint;
            sMImage = sMImage + "<td align=center width=15 background=images/tu_back.gif height=120><img src=images/bar.gif width=15 height=" + sHeight + "  title=访问量：" + aMonth[vL].ToString() + "><br>" + vL + "</td>";
        }
        sMImage = sMImage + "<td>单位（月）</td></tr></table></td></tr></table>";

        //取出所有24小四的统计图表
        MaxMonth = 0;
        vSql = "select cM.cMonth,cM.cCMC,MaxNum.MaxCount from cM,(select Max(cCMC) as MaxCount from cM) as MaxNum order by cM.cMonth asc";
        dt1 = ii.Find(vSql);
        aMonth.Clear();
        aMonth.Add("0");
        for (int vS = 1; vS <= 12; vS++)
        {
            aMonth.Add(dt1.Rows[vS - 1]["cCMC"].ToString());
            MaxMonth = Int32.Parse(dt1.Rows[vS - 1]["MaxCount"].ToString());
        }
        //算出图例最大值的比例
        if (MaxMonth == 0)
            MaxMonth = 1;
        vPoint = 100.00 / MaxMonth;
        sMImage = sMImage + "<table border=0 cellpadding=2><tr><td align=center>访问量12个月分配图表</td></tr><tr><td align=center><table align=center><tr valign=bottom ><td valign=top><table border='0' align='center' cellpadding='0' cellspacing='0' height='100'><tr><td height='25' valign=top nowrap>" + MaxMonth + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.75 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.5 + "</td></tr><tr><td  height='25' valign=top nowrap>" + MaxMonth * 0.25 + "</td></tr></table></td>";

        for (int vL = 1; vL <= 12; vL++)
        {
            double sHeight = Int32.Parse(aMonth[vL].ToString()) * vPoint;
            sMImage = sMImage + "<td align=center width=15 background=images/tu_back.gif height=120><img src=images/bar.gif width=15 height=" + sHeight + "  title=访问量：" + aMonth[vL].ToString() + "><br>" + vL + "</td>";

        }
        sMImage = sMImage + "<td>单位（月）</td></tr></table></td></tr></table>";

        aMonth.Clear();
        dt1.Dispose();

        title = "按月统计信息";
        strval = sMImage;
    }

    /// <summary>
    /// 系统浏览器
    /// </summary>
    private void Sb_OSExp()
    {
        int MaxValue = 0;
        //系统
        string oSql = "select cO.cOS,cO.cCOC,MaxNum.MaxCount from cO,(select Max(cCOC) as MaxCount from cO) as MaxNum order by cO.cCOC desc";

        DataTable dt1 = new DataTable();
        dt1 = ii.Find(oSql);
        string[,] aOS = new string[7, 2];

        for (int i = 0; i <= 6 && i < dt1.Rows.Count; i++)
        {
            aOS[i, 0] = dt1.Rows[i]["cOS"].ToString();
            aOS[i, 1] = dt1.Rows[i]["cCOC"].ToString();
            MaxValue = Int32.Parse(dt1.Rows[i]["MaxCount"].ToString());
        }

        if (MaxValue == 0)
            MaxValue = 1;

        double vPoint = 150.00 / MaxValue;

        string sImage = "<table border=0 cellpadding=2><tr><td width=50% align=center>系统统计图表</td><td width=50% align=center>浏览器统计图表</td></tr><tr valign=top><td height=22 align=center><table align=center>";

        for (int vL = 0; vL <= 6; vL++)
        {
            double sHeight = Int32.Parse(aOS[vL, 1]) * vPoint;
            sImage = sImage + "<tr valign=cente><td align=right nowap>" + aOS[vL, 0] + "</td><td align=left background=images/tu_back_2.gif valign=middle width=150><img src=images/bar2.gif width=" + sHeight + " height=15  title=访问量：" + aOS[vL, 1] + "></td><td nowap>" + aOS[vL, 1] + "</td></tr>";
        }

        sImage = sImage + "</table></td><td align=center>";

        //浏览器
        oSql = "select cE.cExplorer,cE.cCEC,MaxNum.MaxCount from cE,(select Max(cCEC) as MaxCount from cE) as MaxNum order by cE.cCEC desc";

        dt1 = ii.Find(oSql);

        string[,] aExp = new string[7, 2];
        for (int i = 0; i <= 6 && i < dt1.Rows.Count; i++)
        {
            aExp[i, 0] = dt1.Rows[i]["cExplorer"].ToString();
            aExp[i, 1] = dt1.Rows[i]["cCEC"].ToString();
            MaxValue = Int32.Parse(dt1.Rows[i]["MaxCount"].ToString());
        }
        if (MaxValue == 0)
            MaxValue = 1;

        vPoint = 150.00 / MaxValue;

        sImage = sImage + "<table align=center>";

        for (int vL = 0; vL <= 6; vL++)
        {
            double sHeight = Int32.Parse(aExp[vL, 1]) * vPoint;
            sImage = sImage + "<tr valign=bottom ><td align=right nowap>" + aExp[vL, 0] + "</td><td align=left background=images/tu_back_2.gif valign=middle width=150><img src=images/bar2.gif width=" + sHeight + " height=15  title=访问量：" + aExp[vL, 1] + "></td><td nowap>" + aExp[vL, 1] + "</td></tr>";
        }

        sImage = sImage + "</table></td></tr></table>";

        dt1.Dispose();
        title = "按操作系统和浏览器统计";
        strval = sImage;
    }

    /// <summary>
    /// 地区统计
    /// </summary>
    private void Sb_Where()
    {
        int MaxValue = 0;
        string page = "暂无数据";
        string oSql = "select cW.cWhere,cW.cCWC,MaxNum.MaxCount from cW,(select Max(cCWC) as MaxCount from cW) as MaxNum order by cW.cCWC desc";

        int vI = 0;
        if (Request["AbsolutePage"] != null)
            vI = Int32.Parse(Request["AbsolutePage"].ToString()) - 1;
        if (vI < 0) vI = 0;
        string[,] aWhere = new string[20, 2];
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(oSql);
        int lcount = 0;
        if (dt1.Rows.Count > 0)
        {
            int vk = 20 * vI;
            for (int vJ = 0; vJ < 20 && vk < dt1.Rows.Count; vJ++)
            {
                aWhere[vJ, 0] = dt1.Rows[vk]["cWhere"].ToString();
                aWhere[vJ, 1] = dt1.Rows[vk]["cCWC"].ToString();
                MaxValue = Int32.Parse(dt1.Rows[vk]["MaxCount"].ToString());
                vk++;
                lcount = vJ;
            }
            int vPageCount = 0;
            vPageCount = dt1.Rows.Count / 20;
            if (dt1.Rows.Count % 20 != 0)
                vPageCount = vPageCount + 1;
            page = PageSplit(vI + 1, vPageCount, "where");
        }

        if (MaxValue == 0) MaxValue = 1;
        double vPoint = 150.00 / MaxValue;

        string sWhereImage = "<table width=100% border=0 cellpadding=2><tr><td align=center>访问者统计图表</td></tr><tr><td align=center><table align=center>";

        for (int vL = 0; vL <= lcount; vL++)
        {
            double sHeight = Int32.Parse(aWhere[vL, 1]) * vPoint;
            if (Math.Round(sHeight) == 0)
                vL = 20;
            else
                sWhereImage = sWhereImage + "<tr valign=bottom ><td nowap>" + aWhere[vL, 0] + "</td><td align=left background=images/tu_back_2.gif valign=middle width=150><img src=images/bar2.gif width=" + sHeight + " height=15  title=访问量：" + aWhere[vL, 1] + "></td><td nowap>" + aWhere[vL, 1] + "</td></tr>";
        }

        sWhereImage = sWhereImage + "</table><br>";
        sWhereImage = sWhereImage + page + "</td></tr></table>";

        dt1.Dispose();

        title = "访问者地区统计";
        strval = sWhereImage;
    }

    /// <summary>
    /// 来源统计
    /// </summary>
    private void Sb_Referer()
    {
        int MaxValue = 0;
        string page = "暂无数据";
        string oSql = "select cR.cReferer,cR.cCRC,MaxNum.MaxCount from cR,(select Max(cCRC) as MaxCount from cR) as MaxNum order by cR.cCRC desc";

        int vI = 0;
        if (Request["AbsolutePage"] != null)
            vI = Int32.Parse(Request["AbsolutePage"].ToString()) - 1;
        if (vI < 0) vI = 0;

        string[,] aWhere = new string[20, 2];
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(oSql);
        int lcount = 0;
        if (dt1.Rows.Count > 0)
        {
            int vk = 20 * vI;
            for (int vJ = 0; vJ < 20 && vk < dt1.Rows.Count; vJ++)
            {
                aWhere[vJ, 0] = dt1.Rows[vk]["cReferer"].ToString();
                aWhere[vJ, 1] = dt1.Rows[vk]["cCRC"].ToString();
                MaxValue = Int32.Parse(dt1.Rows[vk]["MaxCount"].ToString());
                vk++;
                lcount = vJ;
            }
            int vPageCount = 0;
            vPageCount = dt1.Rows.Count / 20;
            if (dt1.Rows.Count % 20 != 0)
                vPageCount = vPageCount + 1;
            page = PageSplit(vI + 1, vPageCount, "referer");
        }

        if (MaxValue == 0) MaxValue = 1;
        double vPoint = 150.00 / MaxValue;

        string sReImage = "<table width=100% border=0 cellpadding=2><tr><td align=center>访问者来源统计图表</td></tr><tr><td align=center><table align=center>";

        for (int vL = 0; vL <= lcount; vL++)
        {
            if (aWhere[vL, 1] != "")
            {
                double sHeight = Int32.Parse(aWhere[vL, 1]) * vPoint;
                if (Math.Round(sHeight) == 0)
                    vL = 20;
                else
                    sReImage = sReImage + "<tr valign=bottom ><td nowap><a href='" + aWhere[vL, 0] + "' target=_blank title='" + aWhere[vL, 0] + "'>" + ((aWhere[vL, 0].Length) > 27 ? aWhere[vL, 0].Substring(0, 27) : aWhere[vL, 0]) + "</a></td><td align=left background=images/tu_back_2.gif valign=middle width=150><img src=images/bar2.gif width=" + sHeight + " height=15  title=访问量：" + aWhere[vL, 1] + "></td><td nowap>" + aWhere[vL, 1] + "</td></tr>";
            }
        }

        sReImage = sReImage + "</table><br>";
        sReImage = sReImage + page + "</td></tr></table>";

        dt1.Dispose();
        title = "访问者来源统计";
        strval = sReImage;
    }

    /// <summary>
    /// 最后来访者信息
    /// </summary>
    private void Sb_Last20()
    {
        string vSql = "select * from Last20Vister where cCOS<>'' and cCEXP<>'' order by cCT desc,id asc";
        string ShowMess = "暂无数据";
        DataTable dt1 = new DataTable();
        dt1 = ii.Find(vSql);
        if (dt1.Rows.Count > 0)
        {
            ShowMess = "<table width=100% border=0 cellpadding=2 cellspacing=1><tr align=center ><td width=12% >时间</td><td width=13% >操作系统</td><td width=21% >浏览器</td><td width=13% >地区</td><td width=14% >IP地址</td><td width=27% >来源</td></tr>";
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                if (dt1.Rows[i]["cCT"].ToString() == "")
                    i = dt1.Rows.Count;
                else
                {
                    string date = dt1.Rows[i]["cCT"].ToString();
                    date = date.Substring(0, date.IndexOf(" "));
                    ShowMess = ShowMess + "<tr align=center><td width='12%'>" + date + "</td><td  width='13%'>" + dt1.Rows[i]["cCOS"].ToString() + "</td><td width='21%'>" + dt1.Rows[i]["cCEXP"].ToString() + "</td><td width='13%'>" + dt1.Rows[i]["cCW"].ToString() + "</td><td width='14%'>" + dt1.Rows[i]["cCIP"].ToString() + "</td><td width='27%' align=left><a href=\"" + dt1.Rows[i]["cCR"].ToString() + "\" target=_blank  title=\"" + dt1.Rows[i]["cCR"].ToString() + "\">" + ((dt1.Rows[i]["cCR"].ToString().Length) > 27 ? dt1.Rows[i]["cCR"].ToString().Substring(0, 27) : dt1.Rows[i]["cCR"].ToString()) + "...</a></td></tr>";
                }
            }
            ShowMess = ShowMess + "</table>";
        }

        dt1.Dispose();
        title = "最后" + dt1.Rows.Count.ToString() + "位来访者信息";
        strval = ShowMess;
    }

    /// <summary>
    /// 一月有多少天
    /// </summary>
    /// <param name="Mon">指定月份</param>
    /// <returns></returns>
    private int Fn_MaxDay(int Mon)
    {
        int m = 30;
        switch (Mon)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                m = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                m = 30;
                break;
            case 2:
                if (DateTime.Now.Year % 4 == 0)
                    m = 29;
                else
                    m = 28;
                break;
            default:
                m = 30;
                break;
        }
        return m;
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="vI">当前页</param>
    /// <param name="vPageCount">总页数</param>
    /// <param name="cstate">链接参数</param>
    /// <returns></returns>
    private string PageSplit(int vI, int vPageCount, string cstate)
    {
        int Sp = 0;
        if (vI % 10 == 0)
            Sp = vI / 10;
        else
            Sp = vI / 10 + 1;
        int PageStart = (Sp - 1) * 10 + 1;
        int PageEnd = Sp * 10;
        string strSplit = "<table width=500 align=center><tr><td align=center>共" + vPageCount + "页 当前第" + vI + "页 &nbsp; &nbsp;<a href=CountShow.aspx?act=" + cstate + "&AbsolutePage=1><font face=webdings title=第一页>9</font></a>&nbsp;";
        if (Sp > 1)
            strSplit = strSplit + "<a href='CountShow.aspx?act=" + cstate + "&AbsolutePage=" + (PageStart - 10) + "'><font face=webdings title=前十页>7</font></a>&nbsp;";
        for (int j = PageStart; j <= PageEnd; j++)
        {
            if (j > vPageCount)
                j = PageEnd + 1;
            else
            {
                if (j != vI)
                    strSplit = strSplit + "<a href='CountShow.aspx?act=" + cstate + "&AbsolutePage=" + j + "'>[" + j + "]</a>&nbsp;";
                else
                    strSplit = strSplit + "<font color=red>[" + j + "]</font>&nbsp;";
            }
        }
        if (Sp * 10 < vPageCount)
            strSplit = strSplit + "<a href=CountShow.aspx?act=" + cstate + "&AbsolutePage=" + (PageStart + 10) + "><font face=webdings title=后十页>8</font></a>";
        strSplit = strSplit + "<a href='CountShow.aspx?act=" + cstate + "&AbsolutePage=" + vPageCount + "'><font face=webdings title='最后一页'>:</font></a></td></tr></table>";

        return strSplit;
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        string wname = this.WebName.Value.Trim().Replace("'", "''");
        string wurl = this.WebUrl.Value.Trim().Replace("'", "''");
        string wemail = this.WebEmail.Value.Trim().Replace("'", "''");
        string wexp = this.WebExp.Value.Trim().Replace("'", "''");
        string wuser = this.WebMaster.Value.Trim().Replace("'", "''");
        int lnum = 20;
        try
        {
            lnum = Int32.Parse(this.lastnum.Value.Trim().Replace("'", "''"));

            string sql = "insert into WebInfo (WebName,WebUrl,WebEmail,WebExp,WebMaster,WebCountTime) ";
            sql = sql + " values ('" + wname + "','" + wurl + "','" + wemail + "','" + wexp + "','" + wuser + "','" + DateTime.Now.ToString() + "')";
            DataTable dt = new DataTable();
            dt = ii.Find("select * from WebInfo");
            if (dt.Rows.Count > 0)
                sql = "update WebInfo set WebName='" + wname + "',WebUrl='" + wurl + "',WebEmail='" + wemail + "',WebExp='" + wexp + "',WebMaster='" + wuser + "'";

            ii.ExecBool(sql);

            dt = ii.Find("select * from Last20Vister order by cCT asc");
            int ct = dt.Rows.Count;

            if (lnum > ct)
            {
                int nums = lnum - ct;
                for (int i = 0; i < nums; i++)
                {
                    ii.ExecBool("insert into Last20Vister (cCT,cCIP,cCW,cCOS,cCEXP,cCR) values ('" + DateTime.Now.AddSeconds(i).ToString() + "','','','','','')");
                }
            }
            else
            {
                int nums = ct - lnum;
                for (int i = 0; i < nums; i++)
                {
                    ii.ExecBool("delete from Last20Vister where [id]=" + dt.Rows[i]["id"].ToString());
                }
            }
            dt.Dispose();

            Response.Redirect(url + "?act=admin");
        }
        catch (Exception ex)
        {
            Response.Write("ERR:" + ex.Message);
            return;
        }
    }
}
